//
//  ZH_FeaturesSelectTableViewCell.m
//  HenticaSample
//
//  Created by zhangliwen on 2018/5/2.
//  Copyright © 2018年 HenticaSample. All rights reserved.
//

#import "ZH_FeaturesSelectTableViewCell.h"
#import "ZH_FeaturesSelectCollectionViewCell.h"
@interface ZH_FeaturesSelectTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ZH_FeaturesSelectTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_FeaturesSelectTableViewCell";
    ZH_FeaturesSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_FeaturesSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
- (void)loadSubviewConstraints{

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(FITSCALE(0));
        make.left.equalTo(self.contentView).offset(FITWITH(0));
        make.right.equalTo(self.contentView).offset(FITWITH(0));
        make.bottom.equalTo(self.contentView).offset(FITHEIGHT(0));
        make.height.mas_equalTo(FITSCALE(100));
    }];

}
///更新ui
- (void)updateUiForData
{
    [self.collectionView reloadData];
}
///更新ui
- (void)updateUiForData:(NSMutableArray *)datas
{
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:datas];
    [self.collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.onButtonClick) {
        self.onButtonClick(indexPath.row);
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat width = (kMainScreenWidth - FITSCALE(26)) / 5;
    CGSize size = CGSizeMake(width, FITSCALE(100));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return HEIGHT_TRANSFORMATION(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ZH_FeaturesSelectCollectionViewCell *cell = [ZH_FeaturesSelectCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
            [cell updateUiWithImage:self.dataArray[indexPath.row][0] andTitle:self.dataArray[indexPath.row][1]];
//        [cell updateUiWithImage:@"优选00" andTitle:@"优选"];
         return cell;
    }
    
    return nil;
    
}
#pragma mark -- getter,setter
///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];
        collectionView.scrollEnabled = NO;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_FeaturesSelectCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}

///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}


@end
